<?
	require_once('../inc.php');
	
	//print_r($_REQUEST);
	if(isset( $_REQUEST['id']) && $_REQUEST['id'] != '' )
	{
		$rs = User::Get($_REQUEST['id']);
		if($rs)
		{
			$user = new User($rs);
		}
	}
	
	if(isset($_REQUEST['IsPostBack']) && $_REQUEST['IsPostBack'] == 'true')
	{
		if(isset( $_REQUEST['id']) && $_REQUEST['id'] != '' )
		{
			$ValidationErrors = User::Validate($_REQUEST);
			if($ValidationErrors == null)
			{
					if(User::Edit($_REQUEST) == null)
					{
						header('location: index.php');
						die;
					}else{
						echo "echo";
					}
			}
			$user = new User($_REQUEST);
		}else{
			if(User::Validate($_REQUEST) == null)
			{
				User::Create($_REQUEST);
				header('location: index.php');
				die;
			}
			$user = new User($_REQUEST);
		}
	}
	
	if(!isset($user)) $user = new User();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Edit: <?=$user->FirstName?></title>
    <style type="text/css">
		.error {
			color: red;
		}
		.iError {
			border-color: red;
			background-color: #FEE;
		}
		form div {
			margin: 2px 2px 2px 2px;
			clear:both;
		}
		label {
			display: block;
			width: 100px;
			text-align: right;
			float: left;
			margin-right: 5px;
		}
	</style>
</head>

<body>
	<ul class="error">
    	<? foreach($ValidationErrors as $key => $value) { ?>
        	<li><?=$key?>: <?=$value?> </li>
        <? } ?>
    </ul>
	<form method="post">
    	<input type="hidden" name="id" value="<?=$user->id?>" />
    	<input type="hidden" name="IsPostBack" value="true" />        
        <div>
            <label for="FirstName">First Name:</label>
            <input type="text" name="FirstName" value="<?=$user->FirstName?>" id="FirstName" />
        </div>
        <div>
            <label for="LastName">Last Name:</label>
            <input type="text" name="LastName" value="<?=$user->LastName?>" id="LastName" />
        </div>
        <div>
            <label for="Birthday">Birthday:</label>
            <input
            	class="<?=isset($ValidationErrors['Birthday']) ? 'iError' : ''?>"
            	type="text" name="Birthday"
            	value="<?=$user->Birthday?>" id="Birthday" />
            <? if(isset($ValidationErrors['Birthday'])) { ?><?=$ValidationErrors['Birthday']?><? } ?>
        </div>
        <input type="submit" value="Save" />
    </form>
</body>
</html>